package com.gpb.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class UPayUtils {
	
	/**
	 * 
	 * @param defrayurl 支付url地址
	 * @param orderNo 支付单号
	 * @param orderprice  金额单位 元
	 * @param orderSource  订单来源 1 itv , 3 微信
	 * @param cardNo 卡号
	 * @param idcard  身份证号
	 * @param phone  电话号码
	 * @return {"orderId":"201401071058281","respCode":"10","respMsg":"报文域(证件号码)和(证件类型)必须同时存在"}
	 * @throws Exception
	 */
	public final static Map<String, String> do_defray(String defrayurl,String orderNo,Double orderprice,String orderSource,String cardNo,String idcard,String phone,StringBuffer...result_sb) throws Exception{

		//模拟支付失败
		if(false){
			Map map=new HashMap<String, String>();
			map.put("orderId", orderNo);
			map.put("respCode", "61");
			map.put("respMsg", "该商户暂不支持该卡或卡号输入有误。");
			if(result_sb.length>0){
				result_sb[0].append("{\"orderId\":\""+orderNo+"\",\"respCode\":\"61\",\"respMsg\":\"该商户暂不支持该卡或卡号输入有误。\"}");
			}
			return map;
		}
		//模拟支付成功
		if(false){
			Map map=new HashMap<String, String>();
			map.put("orderId", orderNo);
			map.put("respCode", "00");
			map.put("respMsg", "交易成功(0000000)");
			if(result_sb.length>0){
				result_sb[0].append("{respCode=\"00\", respMsg=\"交易成功(0000000)\", orderId=\""+orderNo+"\"}");
			}
			return map;
		}
		//请求参数
		NameValuePair[] data=new NameValuePair[10];
		data[0]=new NameValuePair("orderNo",orderNo);
		int orderPrice=(int)(orderprice*100);
		data[1]=new NameValuePair("orderPrice",orderPrice+"");//金额 分 整数
		data[2]=new NameValuePair("orderName","广电缴费");
		data[3]=new NameValuePair("orderDescribe","广电缴费");
		data[4]=new NameValuePair("tradeType","5");// 5 担保支付
		data[5]=new NameValuePair("orderSource",orderSource);//订单来源 1 itv , 3 微信
		data[6]=new NameValuePair("certificateType","01");//证件类型
		data[7]=new NameValuePair("certificateNo",idcard);//身份证
		data[8]=new NameValuePair("cardNo",cardNo);//卡号
		data[9]=new NameValuePair("phone",phone);//电话号码
		

		//创建post请求方法
		PostMethod postMethod = new PostMethod(defrayurl);
		//设置到请求方法里面
		postMethod.addParameters(data);
		//设置请求参数编码
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");

		
		//创建http请求客户端
		HttpClient httpClient=new HttpClient();
		//执行请求
		int statusCode = httpClient.executeMethod(postMethod);
		//判断请求结果
		if(statusCode == HttpStatus.SC_OK){
			StringBuffer sb=null;
			if(result_sb.length>0){
				sb=result_sb[0];
			}else{
				sb=new StringBuffer();
			}
			//获取返回流
			InputStream in = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
			String inputLine = null;
			//读取返回流
			 while((inputLine = reader.readLine()) != null){
				 sb.append(inputLine);
			 }
			 in.close();

			return JSONObject.fromObject(sb.toString());
		}else{
			return new HashMap<String, String>();
		}
	}
	
	public static void main(String[] args) {
		try {
			System.out.println(do_defray(null,null,null,null,null,null,null));;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,String> abc=new HashMap<String, String>();
		abc.put("1","2");
		abc.put("21","2");
		System.out.println(abc.toString());
		
	}
}
